//
//  CALayer+Shadow.m
//  QNReuseTool
//
//  Created by rsh on 2019/10/15.
//  Copyright © 2019年 rsh. All rights reserved.
//

#import "CALayer+Shadow.h"

#define RGBACOLOR(r, g, b, a)       [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]

@implementation CALayer (Shadow)

-(void)addShadowWithColor:(CGColorRef)colorRef offset:(CGSize)shadowOffset radius:(CGFloat)shadowRadius opacity:(CGFloat)shadowOpacity{
    self.shadowColor =  colorRef;
    self.shadowOffset = shadowOffset;
    self.shadowRadius = shadowRadius;
    self.shadowOpacity = shadowOpacity;
}

-(void)addDefaultShadow{
//    self.shadowColor =  [UIColor blackColor].CGColor;
//    self.shadowOffset = CGSizeMake(0, 1);
//    self.shadowRadius = 4;
//    self.shadowOpacity = 0.20;
    
    self.shadowColor =  RGBACOLOR(0, 0, 0, 0.15).CGColor;
    self.shadowOffset = CGSizeMake(0,0);
    self.shadowRadius = 5;
    self.shadowOpacity = 1;
}

-(void)addDefaultLightShadow{
    self.shadowColor =  [UIColor blackColor].CGColor;
    self.shadowOffset = CGSizeMake(0, 0);
    self.shadowRadius = 5;
    self.shadowOpacity = 0.075;
}

-(void)removeShadow{
    self.shadowColor =  [UIColor clearColor].CGColor;
    self.shadowRadius = 0;
    self.shadowOpacity = 0;
}


-(void)addShadowToEdge:(UIRectEdge)edge color:(nonnull CGColorRef)cColor opacity:(CGFloat)opacity shadowRadius:(CGFloat)radius shadowOffset:(CGSize)sizeOffset{
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    
    
    if (edge == UIRectEdgeNone) {
        //无
        self.shadowColor = nil;
        self.shadowOpacity = 0.0;
        self.shadowColor = nil;
        self.shadowOpacity = 0.0;
        self.shadowOffset = CGSizeZero;
        self.shadowRadius = 0;
        return;
    }
    
    if (edge == UIRectEdgeAll){
        
        UIBezierPath *path4 = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:radius];
        path = path4;
        
    }else {
        
        CGFloat width = self.frame.size.width;
        CGFloat height = self.frame.size.height;
        
        if (edge & UIRectEdgeTop){
            
            UIBezierPath *path1 = [UIBezierPath bezierPath];
            [path1 moveToPoint:CGPointMake(0, 0)];
            [path1 addLineToPoint:CGPointMake(width, 0)];
            [path1 addLineToPoint:CGPointMake(width, radius)];
            [path1 addLineToPoint:CGPointMake(0, radius)];
            [path1 closePath];
            [path appendPath:path1];
            
        }
        
        if (edge & UIRectEdgeLeft){
            
            UIBezierPath *path2 = [UIBezierPath bezierPath];
            [path2 moveToPoint:CGPointMake(radius, 0)];
            [path2 addLineToPoint:CGPointMake(radius, height)];
            [path2 addLineToPoint:CGPointMake(0, height)];
            [path2 addLineToPoint:CGPointMake(0, 0)];
            [path2 closePath];
            [path appendPath:path2];
        }
        
        if (edge & UIRectEdgeBottom){
            
            UIBezierPath *path0 = [UIBezierPath bezierPath];
            [path0 moveToPoint:CGPointMake(0, height - radius)];
            [path0 addLineToPoint:CGPointMake(width, height - radius)];
            [path0 addLineToPoint:CGPointMake(width, height)];
            [path0 addLineToPoint:CGPointMake(0, height)];
            [path0 closePath];
            [path appendPath:path0];
            
        }
        
        if (edge & UIRectEdgeRight){
            
            UIBezierPath *path3 = [UIBezierPath bezierPath];
            [path3 moveToPoint:CGPointMake(width-radius, 0)];
            [path3 addLineToPoint:CGPointMake(width-radius, height)];
            [path3 addLineToPoint:CGPointMake(width, height)];
            [path3 addLineToPoint:CGPointMake(width, 0)];
            [path3 closePath];
            [path appendPath:path3];
            
        }
    }
    
    self.shadowColor = cColor;
    self.shadowOpacity = opacity;
    self.shadowOffset = sizeOffset;
    self.shadowRadius = radius;
    self.shadowPath = path.CGPath;
}

-(void)addDefaultShadowToEdge:(UIRectEdge)edge{
    
    [self addShadowToEdge:edge
                    color:[UIColor blackColor].CGColor
                  opacity:0.15
             shadowRadius:5
             shadowOffset:CGSizeZero];
    
}

-(void)addDefaultShadowToEdge:(UIRectEdge)edge color:(nonnull CGColorRef)cColor{
    
    [self addShadowToEdge:edge
                    color:cColor
                  opacity:0.15
             shadowRadius:5
             shadowOffset:CGSizeZero];
}

-(void)addDefaultShadowToEdge:(UIRectEdge)edge opacity:(CGFloat)opacity{
    
    [self addShadowToEdge:edge
                    color:[UIColor blackColor].CGColor
                  opacity:opacity
             shadowRadius:5
             shadowOffset:CGSizeZero];
}

-(void)addDefaultShadowToEdge:(UIRectEdge)edge shadowRadius:(CGFloat)radius{
    
    [self addShadowToEdge:edge
                    color:[UIColor blackColor].CGColor
                  opacity:0.15
             shadowRadius:radius
             shadowOffset:CGSizeZero];
    
}

-(void)addDefaultShadowToEdge:(UIRectEdge)edge shadowOffset:(CGSize)sizeOffset{
    
    [self addShadowToEdge:edge
                    color:[UIColor blackColor].CGColor
                  opacity:0.15
             shadowRadius:5
             shadowOffset:sizeOffset];
    
}


@end
